<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>深浅拷贝</title>
  </head>
  <body>
    <script>
      // 浅拷贝

      function shallowClone(obj) {
        const newObj = {};
        for (let prop in obj) {
          if (obj.hasOwnProperty(prop)) {
            newObj[prop] = obj[prop];
          }
        }
        return newObj;
      }

      var obj = {
        age: 18,
        nature: ["smart", "good"],
        names: {
          name1: "fx",
          name2: "xka",
        },
        love: function () {
          console.log("fx is a great girl");
        },
      };
      var newObj = Object.assign({}, fxObj);

      const a1 = ["One", "Two", "Three"];
      const a2 = fxArr.slice(0);
      a2[1] = "love";
      console.log(a1);
      console.log(a2);

      const fxArr = ["One", "Two", "Three"];
      const fxArrs = fxArr.concat();
      fxArrs[1] = "love";
      console.log(fxArr);
      console.log(fxArrs);

      const fxArr = ["One", "Two", "Three"];
      const fxArrs = [...fxArr];
      fxArrs[1] = "love";
      console.log(fxArr);
      console.log(fxArrs);

      // ——————————————————————————————————————————————————————————————————————————————————————————

      /*     // 深拷贝

            const obj1 = {
              name: "A",
              name1: undefined,
              name3: function () {},
              name4: Symbol("A"),
            };

            const obj2 = JSON.parse(JSON.stringify(obj1));
            console.log(obj2); */

      /*    //循环递归
            let obj = {
              name: "小明",
              arr: [10, 20],
              fn: function () {
                console.log(111);
              },
              old: {
                one: "意义",
                two: "带来",
              },
            };

            function a(obj) {
              var b = null;
              if (typeof obj === "object" && obj !== null) {
                b = obj instanceof Array ? [] : {};
                for (const key in obj) {
                  b[key] = a(obj[key]);
                }
              } else {
                b = obj;
              }
              return b;
            }
            let att = new a(obj);
            att.arr[0] = 555;
            console.log(obj);
            console.log(att); */
    </script>
  </body>
</html>
